package lucene.query
{
	import lucene.index.Term;
	import lucene.search.IReader;
	import lucene.search.TermWeight;
	
	import utils.ArrayUtils;
	
	public class TermQuery extends Query
	{
		private var _term:Term;
		private var _docVector:Array;
		private var _termFreqs:Array;
				
		public function TermQuery(term:Term)
		{
			super(this);
			this._term = term;
		}

		override public function createWeight(reader:IReader):*
		{
			this._weight = new TermWeight(this._term, this, reader);
			return this._weight;
		} 

		override public function score(docId:int, reader:IReader):Number
		{
			if (this._docVector[docId] != null) {
				return reader.getSimilarity().tf(this._termFreqs[docId]) * 
						this._weight.getValue() *
						reader.norm (docId, this._term.field) *
						this.getBoost();
			}
			else {
				return 0;	
			}
			
		}

		override public function execute(reader:IReader):void
		{
			var a:Array = reader.termDocs(this._term);
			
			this._docVector = ArrayUtils.flip(a);
			this._termFreqs = reader.termFreqs(this._term);
			
			this._initWeight(reader);
		}
		
		override public function matchedDocs():Array
		{
			return this._docVector;
		}

	}
}